#include "stdafx.h"
#include "road_node_map.h"
#include "clientinterfaces.h"
#include <iltclient.h>
#include "ltclientshell.h"
#include <fstream>
#include "Screen/tex_mgr.h"

void road_node_map::load_road_info( const char* _filename )
{
	std::fstream file_in(_filename);
	try
	{
		int road_size;
		file_in>>road_size;
		for (int i=0;i<road_size;++i)
		{
			LTVector temp_pos;
			file_in>>temp_pos.x;
			file_in>>temp_pos.y;
			file_in>>temp_pos.z;

			LTVector temp_forward;
			file_in>>temp_forward.x;
			file_in>>temp_forward.y;
			file_in>>temp_forward.z;

			temp_forward.Normalize();
			

			LTVector temp_up;
			file_in>>temp_up.x;
			file_in>>temp_up.y;
			file_in>>temp_up.z;

			temp_up.Normalize();

			LTRotation temp_rot(temp_forward,temp_up);	
			road_node temp_node;
			temp_node.set_trans(temp_pos,temp_rot);
			temp_node.set_color();
			m_nodes.push_back(temp_node);
		}
	}
	catch(...)
	{
		file_in.close();
	}


}

road_node_map::road_node_map():
m_car(0),
m_node_index(0),
m_map_height(0.3f),
m_map_wide(0.18f),
m_map_pos_x(0.02f),
m_map_pos_y(0.65f),
m_degree(600.0f),
m_window_w(1024),
m_window_h(768),
m_other_players_num(0)
{
	
	for (int i=0;i<m_show_nodes;++i)
	{
	
		m_map_elements_f[i].set_compote_size(m_map_wide*m_window_w,m_map_height*m_window_h);
		m_map_elements_f[i].set_compote_position(m_map_pos_x*m_window_w,m_map_pos_y*m_window_h);
		
		m_map_elements_f[i].set_color(0,255,255,255);
		m_map_elements_f[i].set_position(0.0,0.0);
		m_map_elements_f[i].set_size(0.04f,0.04f);
		m_map_elements_f[i].set_uv();


		m_map_elements_b[i].set_compote_size(m_map_wide*m_window_w,m_map_height*m_window_h);
		m_map_elements_b[i].set_compote_position(m_map_pos_x*m_window_w,m_map_pos_y*m_window_h);

		m_map_elements_b[i].set_color(0,255,255,255);
		m_map_elements_b[i].set_position(0.0,0.0);
		m_map_elements_b[i].set_size(0.04f,0.04f);
		m_map_elements_b[i].set_uv();

	}
	m_map_back.set_compote_size(m_map_wide*m_window_w,m_map_height*m_window_h);
	m_map_back.set_compote_position(m_map_pos_x*m_window_w,m_map_pos_y*m_window_h);
	m_map_back.set_color(86,64,64,64);
	m_map_back.set_size(1.0f,1.0f);
	m_map_back.set_position(0.5f,0.5f);

	m_map_self.set_compote_size(m_map_wide*m_window_w,m_map_height*m_window_h);
	m_map_self.set_compote_position(m_map_pos_x*m_window_w,m_map_pos_y*m_window_h);
	m_map_self.set_color(128,0,255,0);
	m_map_self.set_size(0.05f,0.05f);
	m_map_self.set_position(0.5f,0.5f);

	m_rounds.set_string("Rounds");
	m_rounds.set_color(160,0,255,0);
	m_rounds.set_pos(m_window_w*0.8f,m_window_h*0.1f);

	for (int i=0;i<m_max_players;++i)
	{
		
		m_players[i].m_map_bmp.set_compote_size(m_map_wide*m_window_w,m_map_height*m_window_h);
		m_players[i].m_map_bmp.set_compote_position(m_map_pos_x*m_window_w,m_map_pos_y*m_window_h);
		m_players[i].m_map_bmp.set_size(0.05f,0.05f);
		switch (i)
		{
		case 0:
			m_players[i].m_map_bmp.set_color(255,255,0,0);
			break;
		case 1:
			m_players[i].m_map_bmp.set_color(255,0,0,255);
			break;
		case 2:
			m_players[i].m_map_bmp.set_color(255,255,255,0);
			break;
		}
	}



}

road_node_map::~road_node_map()
{

}

void road_node_map::draw()
{
	
	
	if (!m_car)
	{
		m_car=g_pLTClient->GetClientObject();
	}
	if (m_nodes.empty())
	{
		return;
	}
	location();


	LTVector _pos;
	LTMatrix _ma;
	LTRotation _rot;

	LTVector _car_pos;
	g_pLTClient->GetObjectPos(m_car,&_car_pos);
   
	g_pLTClient->GetObjectRotation(m_car,&_rot);

	_rot.ConvertToMatrix(_ma);
	_ma.Inverse();
	//_ma.Apply4x4(m_nodes[m_node_index].get_pos(),_pos);
	_ma.Apply4x4(_car_pos,_pos);
	

	


	//前
	for (int i=0;i<m_show_nodes;++i)
	{
		if (m_node_index+i>=m_nodes.size())
		{
			_ma.Apply4x4(m_nodes[m_node_index+i-m_nodes.size()].get_pos(),m_temp_vec[i]);	
		}
		else
			_ma.Apply4x4(m_nodes[m_node_index+i].get_pos(),m_temp_vec[i]);	
	    
	}


	for (int i=1;i<m_show_nodes;++i)
	{
			float temp_x=(m_temp_vec[i].x-_pos.x)/m_degree+0.5f;
			float temp_z=(m_temp_vec[i].z-_pos.z)/m_degree+0.5f;
			temp_z=1.0f-temp_z;
			if(temp_x>=0.02f&&temp_x<=0.98f&&temp_z>=0.02f&&temp_z<=0.98f)
			{
				m_map_elements_f[i].set_color(250);
				m_map_elements_f[i].set_position(temp_x,temp_z);
				if (i==1)
				{
					m_map_self.set_position(temp_x,temp_z);
				}
				for (int j=0;j<m_other_players_num;++j)
				{
					if (m_node_index+i>=m_nodes.size())
					{
						if(m_players[j].m_current_node==m_node_index+i-m_nodes.size())
						{
							m_players[j].m_map_bmp.set_position(temp_x,temp_z);						
						}						
					}
					else
					{
						if(m_players[j].m_current_node==m_node_index+i)
						{
							m_players[j].m_map_bmp.set_position(temp_x,temp_z);						
						}						
					}
				}
				
			}
			else
			{
				m_map_elements_f[i].set_color(0);
			}
		
}
			
		
	


	//后
	
	for (int i=0;i<m_show_nodes;++i)
	{
       int temp_r=m_node_index-i;
		
		if (temp_r<0)
		{
			_ma.Apply4x4(m_nodes[m_nodes.size()+(temp_r)].get_pos(),m_temp_vec[i]);	
		}
		else
		{
			_ma.Apply4x4(m_nodes[temp_r].get_pos(),m_temp_vec[i]);	
		}
			

	}


	for (int i=1;i<m_show_nodes;++i)
	{
		

			float temp_x=(m_temp_vec[i].x-_pos.x)/m_degree+0.5f;
			float temp_z=(m_temp_vec[i].z-_pos.z)/m_degree+0.5f;

			temp_z=1.0f-temp_z;
			if(temp_x>=0.02f&&temp_x<=0.98f&&temp_z>=0.02f&&temp_z<=0.98f)
			{
				m_map_elements_b[i].set_color(250);
				m_map_elements_b[i].set_position(temp_x,temp_z);
				for (int j=0;j<m_other_players_num;++j)
				{
					if (m_node_index-i<0)
					{
						if(m_players[j].m_current_node==m_nodes.size()+m_node_index-i)
						{
							m_players[j].m_map_bmp.set_position(temp_x,temp_z);						
						}						
					}
					else
					{
						if(m_players[j].m_current_node==m_node_index-i)
						{
							m_players[j].m_map_bmp.set_position(temp_x,temp_z);						
						}						
					}
				}
			}
			else
			{
				m_map_elements_b[i].set_color(0);
			}	
		
	}
	
	




	

	
	

	


	
	g_pLTCDrawPrim->SetTransformType(DRAWPRIM_TRANSFORM_SCREEN);
	g_pLTCDrawPrim->SetZBufferMode(DRAWPRIM_NOZ);
	g_pLTCDrawPrim->SetColorOp(DRAWPRIM_NOCOLOROP);

	

	

	
	for (int i=0;i<m_show_nodes;++i)
	
	{
		m_map_elements_f[i].update();
		m_map_elements_b[i].update();

		m_temp_poly[i]=m_map_elements_f[i].m_poly;
		m_temp_poly[i+m_show_nodes]=m_map_elements_b[i].m_poly;
		//m_map_elements_f[i].render();
		//m_map_elements_b[i].render();

	}
	static LT_POLYFT4* temp=&m_temp_poly[0];
	g_pLTCDrawPrim->DrawPrim(temp,m_show_nodes*2);

	m_map_back.update();
	m_map_back.render();

	m_map_self.update();
	m_map_self.render();

	for (int i=0;i<m_other_players_num;++i)
	{
		

		m_players[i].m_map_bmp.update();
		m_players[i].m_map_bmp.render();
	}


	//m_rounds.render();


	
	







}

void road_node_map::relocation()
{
	if (!m_car)
	{
		m_car=g_pLTClient->GetClientObject();
	}
	m_nodes[m_node_index].set_color();
	LTVector m_car_pos;
	g_pLTClient->GetObjectPos(m_car,&m_car_pos);
	float temp_len=10000.0f;
	for (int i=0;i<m_nodes.size();++i)
	{
		if (m_car_pos.Dist(m_nodes[i].get_pos())<temp_len)
		{
			temp_len=m_car_pos.Dist(m_nodes[i].get_pos());
			m_node_index=i;
		}
	}
	m_nodes[m_node_index].set_color(200,200,200,200);



	for (int i=0;i<m_other_players_num;++i)
	{
		
		LTVector m_car_pos1;
		g_pLTClient->GetObjectPos(m_players[i].m_obj,&m_car_pos1);
		float temp_len=10000.0f;
		for (int j=0;j<m_nodes.size();++j)
		{
			if (m_car_pos1.Dist(m_nodes[j].get_pos())<temp_len)
			{
				temp_len=m_car_pos.Dist(m_nodes[j].get_pos());
				m_players[i].m_current_node=j;
			}
		}

	}




}

void road_node_map::debug_render()
{

	if (!m_tex)
	{
		m_tex=tex_mgr::single().get_tex("Dir");
	}
	if (m_nodes.empty())
	{
		return;
	}
	location();
	g_pLTCDrawPrim->SetTransformType(DRAWPRIM_TRANSFORM_WORLD);
	g_pLTCDrawPrim->SetZBufferMode(DRAWPRIM_ZRW);
	g_pLTCDrawPrim->SetColorOp(DRAWPRIM_MODULATE);
	g_pLTCDrawPrim->SetTexture(m_tex);

	for (std::vector<road_node>::iterator it =m_nodes.begin();
		it!=m_nodes.end();++it)
	{
		g_pLTCDrawPrim->SetTexture(m_tex);
		it->render();
	}


}

void road_node_map::location()
{
	if (!m_car)
	{
		m_car=g_pLTClient->GetClientObject();
	}
	//计算自身所在node
	LTVector m_car_pos;
	g_pLTClient->GetObjectPos(m_car,&m_car_pos);

	if (m_node_index==0&&m_node_index<m_nodes.size()-1)
	{
		if (m_car_pos.Dist(m_nodes[m_node_index].get_pos())>
			m_car_pos.Dist(m_nodes[m_node_index+1].get_pos()))
		{
			m_nodes[m_node_index].set_color();
			m_node_index++;
			m_nodes[m_node_index].set_color(200,200,200,200);
		}
		else if (m_car_pos.Dist(m_nodes[m_node_index].get_pos())>
			m_car_pos.Dist(m_nodes.back().get_pos()))
		{
			m_nodes[m_node_index].set_color();
			m_node_index=m_nodes.size()-1;
			m_nodes[m_node_index].set_color(200,200,200,200);
		}
	
	}
	else  if (m_node_index==m_nodes.size()-1&&m_node_index>0)
	{
		if (m_car_pos.Dist(m_nodes[m_node_index].get_pos())>
			m_car_pos.Dist(m_nodes[m_node_index-1].get_pos()))
		{
			m_nodes[m_node_index].set_color();
			m_node_index--;
			m_nodes[m_node_index].set_color(200,200,200,200);
		}
		else if (m_car_pos.Dist(m_nodes[m_node_index].get_pos())>
			m_car_pos.Dist(m_nodes[0].get_pos()))
		{
			m_nodes[m_node_index].set_color();
			m_node_index=0;
			m_nodes[m_node_index].set_color(200,200,200,200);
		}

	
	}
	else if (m_node_index>0&&m_node_index<m_nodes.size()-1)
	{
		if (m_car_pos.Dist(m_nodes[m_node_index].get_pos())>
			m_car_pos.Dist(m_nodes[m_node_index+1].get_pos()))
		{
			m_nodes[m_node_index].set_color();
			m_node_index++;
			m_nodes[m_node_index].set_color(200,200,200,200);
		}
		else if (m_car_pos.Dist(m_nodes[m_node_index].get_pos())>
			m_car_pos.Dist(m_nodes[m_node_index-1].get_pos()))
		{
			m_nodes[m_node_index].set_color();
			m_node_index--;
			m_nodes[m_node_index].set_color(200,200,200,200);
		}

	}


	//计算对手所在node
	for (int i=0;i<m_other_players_num;++i)
	{
		LTVector m_car_pos1;
		g_pLTClient->GetObjectPos(m_players[i].m_obj,&m_car_pos1);
		int m_node_index1=m_players[i].m_current_node;

		if (m_node_index1==0&&m_node_index1<m_nodes.size()-1)
		{
			if (m_car_pos1.Dist(m_nodes[m_node_index1].get_pos())>
				m_car_pos1.Dist(m_nodes[m_node_index1+1].get_pos()))
			{
				m_node_index1++;
			
			}
			else if (m_car_pos1.Dist(m_nodes[m_node_index1].get_pos())>
				m_car_pos1.Dist(m_nodes.back().get_pos()))
			{
				m_node_index1=m_nodes.size()-1;
	
			}

		}
		else  if (m_node_index1==m_nodes.size()-1&&m_node_index1>0)
		{
			if (m_car_pos1.Dist(m_nodes[m_node_index1].get_pos())>
				m_car_pos1.Dist(m_nodes[m_node_index1-1].get_pos()))
			{
				m_node_index--;
			}
			else if (m_car_pos1.Dist(m_nodes[m_node_index1].get_pos())>
				m_car_pos1.Dist(m_nodes[0].get_pos()))
			{
				m_node_index1=0;
			}


		}
		else if (m_node_index>0&&m_node_index<m_nodes.size()-1)
		{
			if (m_car_pos1.Dist(m_nodes[m_node_index1].get_pos())>
				m_car_pos1.Dist(m_nodes[m_node_index1+1].get_pos()))
			{
		
				m_node_index1++;
			
			}
			else if (m_car_pos1.Dist(m_nodes[m_node_index1].get_pos())>
				m_car_pos1.Dist(m_nodes[m_node_index1-1].get_pos()))
			{
				
				m_node_index1--;
		
			}

		}
		m_players[i].m_current_node=m_node_index1;

	}




}

void road_node_map::add_player( HOBJECT _obj,const std::string& _name )
{
	if (_obj&&m_other_players_num<m_max_players)
	{
		m_players[m_other_players_num].m_obj=_obj;
		m_players[m_other_players_num].m_name=_name;
		m_other_players_num++;
	}
	relocation();




}
